/**
 * @author fengbin
 * 2009-08-10 
 */
package com.wsj.web.action.search;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.wsj.service.IHomeSiteSearchHotKeyServ;
import com.wsj.util.exception.BusinessException;
import com.wsj.util.page.PageResultSet;
import com.wsj.web.action.BaseAction;
import com.wsj.web.constant.page.PageContent;
import com.wsj.web.form.search.HomeSiteSearchHotKeyForm;
import com.wsj.web.vo.homeSite.ComAdType;
import com.wsj.web.vo.homeSite.HomeSiteSearchHotKey;

/**
 * @author fengbin
 *  2009-08-10 
 */
public class HomeSiteSearchHotKeyAction extends BaseAction 
{
	/*****
	 * 搜索热门关键字接口
	 */
	IHomeSiteSearchHotKeyServ homeSiteSearchHotKeyServ;
	
     //热键搜索管理Action
	public ActionForward toAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception 
	{
		
		 String toPage="homeSiteSearchHotKeyAdd";
		
		 //加载出热键所属的板块列表
		 List li= this.getHomeSiteSearchHotKeyServ().loadComAdTypeList();
		 
		 request.setAttribute("comAdTypeList", li);
		 
		 return mapping.findForward(toPage);
	}
	
	
	/******
	 * 添加热门关键字
	 * @return
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception 
	{
		String toPage="homeSiteSearchHotKeyAdd";
		HomeSiteSearchHotKeyForm homeSiteSearchHotKeyForm=(HomeSiteSearchHotKeyForm)form; 
		
		if(this.checkHomeSiteSearchHotKeyForm(request,homeSiteSearchHotKeyForm,"add"))
		{
			HomeSiteSearchHotKey homeSiteSearchHotKey= new HomeSiteSearchHotKey();
			BeanUtils.copyProperties(homeSiteSearchHotKey, homeSiteSearchHotKeyForm);
			
			homeSiteSearchHotKey.setId(null);
			ComAdType comAdType = this.getHomeSiteSearchHotKeyServ().loadComAdTypeById(homeSiteSearchHotKeyForm.getComAdTypeId());
			homeSiteSearchHotKey.setComAdType(comAdType);
			homeSiteSearchHotKey.setCreateTime(new Date());
			boolean isok=this.getHomeSiteSearchHotKeyServ().addHomeSiteSearchHotKey(homeSiteSearchHotKey);
			
			if(isok)
			{
				this.setPopMsg(request, "msg", "添加成功!");
				request.setAttribute("homeSiteSearchHotKeyForm", new HomeSiteSearchHotKeyForm());
				
				 //加载出热键所属的板块列表
				 List li= this.getHomeSiteSearchHotKeyServ().loadComAdTypeList();
				 request.setAttribute("comAdTypeList", li);
				
			}
			else
			{
				this.setPopMsg(request, "msg", "添加失败!");
				toPage="addHomeSiteSearchHotKey";
			}
			
		}
		return mapping.findForward(toPage);
	}
	
	
	
	/******
	 * 处理多项 
	 */
	public  ActionForward dealWithAll(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception 
	{
		  String[] checkboxparam=request.getParameterValues("checkboxparam");
	        
		  String doit=request.getParameter("doit");
		  Integer[] ids=null;
	        
	        if(checkboxparam!=null&&checkboxparam.length>0)
	        {
	            ids=new Integer[checkboxparam.length];
	            
	            for(int j=0;j<checkboxparam.length;j++)
	            {
	               ids[j]=Integer.valueOf(checkboxparam[j]);
	            }
	        }
	        
	        //审核
	        if(doit.equals("shenhe"))
	        {
	            manyDealWithProducts(ids,"shenhe",new Short("1"));
	            this.setPopMsg(request, "msg", "审核成功");
	        }
	        //取消审核
	        if(doit.equals("qxshenhe"))
	        {
	        	manyDealWithProducts(ids,"shenhe",new Short("0"));
	        	this.setPopMsg(request, "msg", "取消审核成功");
	        }
	        //推荐
	        if(doit.equals("tuijian"))
	        {
	        	short tuijian=1;
	        	manyDealWithProducts(ids,"tuijian",tuijian);
	        	this.setPopMsg(request, "msg", "推荐成功");
	        }
	        //取消推荐
	        if(doit.equals("qxtuijian"))
	        {
	        	short qxtuijian=0;
	        	manyDealWithProducts(ids,"tuijian",qxtuijian);
	        	this.setPopMsg(request, "msg", "取消推荐成功");
	        }
	        //删除
	        if(doit.equals("shanchu"))
	        {
	        	short shanchu=1;
	        	manyDealWithProducts(ids,"shanchu",shanchu);
	        	this.setPopMsg(request, "msg", "删除成功");
	        }
	        //取消删除
	        if(doit.equals("qxshanchu"))
	        {
	        	short shanchu=0;
	        	manyDealWithProducts(ids,"shanchu",shanchu);
	        	this.setPopMsg(request, "msg", "取消删除成功");
	        }
	        
	        
	        //批量修改之后,返回处理
	       String toPage=returnPage(request);
	       
	       //加载出热键所属的板块列表
			 List li= this.getHomeSiteSearchHotKeyServ().loadComAdTypeList();
			 
			 request.setAttribute("comAdTypeList", li);
	       
	       return new ActionForward(toPage);
		
	}
	
	
	/*****
	 * 通过返回参数,重新跳转到列表页面
	 * @param request
	 * @return
	 */
	private String returnPage(HttpServletRequest request) 
	{
		String toPage="";
		try {
			
			
			StringBuffer backPage=new StringBuffer("/admin/homeSiteSearchHotKey.do?action=list");
			 String comAdTypeIdStr = request.getParameter("comAdTypeId");
			 if(comAdTypeIdStr!=null && !comAdTypeIdStr.equals("") && !comAdTypeIdStr.equals("-1"))
			 {
				 backPage.append("&comAdTypeId="+comAdTypeIdStr);
			 }

			 String keywordsStr  =  request.getParameter("keywords"); 
			 if(keywordsStr!=null && !keywordsStr.equals(""))
			 {
				 backPage.append("&keywords="+new String(keywordsStr.getBytes("ISO8859-1"),"UTF-8"));
			 }

			 String auditeStr = request.getParameter("audite");
			 if(auditeStr!=null  && !auditeStr.equals(""))
			 {
				 backPage.append("&audite="+auditeStr);
			 }

			 String iscommendStr =  request.getParameter("iscommend");
			 if(iscommendStr!=null  && !iscommendStr.equals(""))
			 {
				 backPage.append("&iscommend="+iscommendStr);
			 }
			 
			 String isDelStr =  request.getParameter("isDel");
			 if(isDelStr!=null  && !isDelStr.equals(""))
			 {
				 backPage.append("&isDel="+isDelStr);
			 }
			 
			 String page= request.getParameter("page");
			 if(page!=null  && !page.equals(""))
			 {
				 backPage.append("&page="+page);
			 }
			 
			 toPage = backPage.toString();
			 
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
	     return toPage;
		
	}


	/*****
	 * 真正的批量处理方法
	 * @param ids
	 * @param string
	 * @param short1
	 */
	private void manyDealWithProducts(Integer[] ids, String doit, Object value) 
	{
		
		 try {
			if(ids!=null&&ids.length>0)
			 {
			     for(int j=0;j<ids.length;j++)
			     {
			    	 
			    	 HomeSiteSearchHotKey homeSiteSearchHotKey  = this.getHomeSiteSearchHotKeyServ().loadHomeSiteSearchHotKeyById(ids[j]); 
			    	 //审核处理
				   if(doit.equals("shenhe"))
				   {
					 homeSiteSearchHotKey.setAudite(Short.valueOf(value.toString()));
				   }
				   //推荐
				   else if(doit.equals("tuijian"))
				   {
					 homeSiteSearchHotKey.setIscommend(Short.valueOf(value.toString()));  
				   }
				   //删除
				   else if(doit.equals("shanchu"))
				   {
					 homeSiteSearchHotKey.setIsDel(Short.valueOf(value.toString()));
				   }
				   
				   boolean isok=this.getHomeSiteSearchHotKeyServ().updateHomeSiteSearchHotKey(homeSiteSearchHotKey);
				   
				   
			     }
			 }
			
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		  
		
	}


	/*******
	 * 加载关键字列表
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception 
	{
		
		String toPage="homeSiteSearchHotKeyList";
		HomeSiteSearchHotKeyForm homeSiteSearchHotKeyForm=new HomeSiteSearchHotKeyForm();
		
		//分类
	    String  comAdTypeId = request.getParameter("comAdTypeId");
	    if(comAdTypeId!=null && !comAdTypeId.equals("") && !comAdTypeId.equals("-1"))
	    {
	    	homeSiteSearchHotKeyForm.setComAdTypeId(Integer.valueOf(comAdTypeId));
	    }
	    
	    //关键字
		String keywords  = request.getParameter("keywords");
		if(keywords!=null  && !keywords.equals(""))
		{
			homeSiteSearchHotKeyForm.setKeywords(new String(keywords.getBytes("ISO8859-1"),"UTF-8"));
		}
		
       //是否审核
	    String audite =  request.getParameter("audite");
	    if(audite!=null && !audite.equals("")  && !audite.equals("-1"))
	    {
	    	homeSiteSearchHotKeyForm.setAudite(new Short(audite));
	    }

	    //是否推荐
	    String iscommend  =  request.getParameter("iscommend");
	    if(iscommend!=null  && !iscommend.equals("") && !iscommend.equals("-1"))
	    {
	    	homeSiteSearchHotKeyForm.setIscommend(new Short(iscommend));
	    }
    
	    //是否审核
	    String isDel = request.getParameter("isDel");
	    if(isDel!=null  && !isDel.equals("") && !isDel.equals("-1"))
	    {
	    	homeSiteSearchHotKeyForm.setIsDel(new Short(isDel));
	    }
	    
	    //页码,显示数
	    Integer page=PageContent.pageValue;
	    String pageStr  =  request.getParameter("page");
	    if(pageStr!=null  && !pageStr.equals(""))
	    {
	    	page = Integer.valueOf(pageStr);
	    }
	    
	    //显示数
	    Integer pageSize=PageContent.pageSizeValue;
	    
	    
		this.loadHomeSiteSearchHotKeyList(request ,page,pageSize, homeSiteSearchHotKeyForm);
		
		 //加载出热键所属的板块列表
		 List li= this.getHomeSiteSearchHotKeyServ().loadComAdTypeList();
		 
		 request.setAttribute("comAdTypeList", li);
		
		return mapping.findForward(toPage);
	}
	
	
	/******
	 * 加载关键字列表
	 * @param homeSiteSearchHotKeyForm
	 */
    private void loadHomeSiteSearchHotKeyList(HttpServletRequest request,Integer page,Integer pageSize,HomeSiteSearchHotKeyForm homeSiteSearchHotKeyForm) 
    {
    	
		try {
			
			//处理搜索分类
			if(homeSiteSearchHotKeyForm.getComAdTypeId()!=null && homeSiteSearchHotKeyForm.getComAdTypeId().intValue() == -1)
			{
				homeSiteSearchHotKeyForm.setComAdTypeId(null);
			}
			
			//是否审核
			if(homeSiteSearchHotKeyForm.getAudite()!=null && homeSiteSearchHotKeyForm.getAudite().intValue() == -1)
			{
				homeSiteSearchHotKeyForm.setAudite(null);
			}
			

//			是否推荐
			if(homeSiteSearchHotKeyForm.getIscommend()!=null && homeSiteSearchHotKeyForm.getIscommend().intValue() == -1)
			{
				homeSiteSearchHotKeyForm.setIscommend(null);
			}
			
//			是否删除
			if(homeSiteSearchHotKeyForm.getIsDel()!=null && homeSiteSearchHotKeyForm.getIsDel().intValue() == -1)
			{
				homeSiteSearchHotKeyForm.setIsDel(null);
			}
			
			PageResultSet pageResultSet = this.getHomeSiteSearchHotKeyServ().loadHomeSiteSearchHotKeyList(page, pageSize,homeSiteSearchHotKeyForm);
			
			//搜索form
			request.setAttribute("homeSiteSearchHotKeyForm", homeSiteSearchHotKeyForm);
			
			if(pageResultSet!=null && pageResultSet.getResult()!=null  && !pageResultSet.getResult().isEmpty())
			{
				
				
				request.setAttribute("homeSiteSearchHotKeyList", pageResultSet.getResult());
				
//			设置上一页
			      request.setAttribute(PageContent.backPage,pageResultSet.getBackPage());
			      //下一页
			      request.setAttribute(PageContent.nextPage,pageResultSet.getNextPage());
			      //当前页
			      request.setAttribute(PageContent.curPage,pageResultSet.getCurPage());
			      
			      //总共页数
			      request.setAttribute(PageContent.allPage,pageResultSet.getAllPage());
			      
			      //总记录数
			      request.setAttribute(PageContent.recordNum,pageResultSet.getRecordNum());
			      
			      //开始行码
			      request.setAttribute(PageContent.startRow,pageResultSet.getStartRow());
			      
			      //结束行码
			      request.setAttribute(PageContent.endRow,pageResultSet.getEndRow());
			      
			}
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

    
    /******
     * 跳转到修改热门关键字页面
     */
   public ActionForward toUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception 
	{
   	   String toPage="homeSiteSearchHotKeyUpdate";
	   //通过id,加载出热门关键字对象
	   String id=request.getParameter("id");
	   HomeSiteSearchHotKeyForm homeSiteSearchHotKeyForm=new HomeSiteSearchHotKeyForm();
	   if(id!=null  && !id.equals(""))
	   {
		   HomeSiteSearchHotKey homeSiteSearchHotKey = this.getHomeSiteSearchHotKeyServ().loadHomeSiteSearchHotKeyById(Integer.valueOf(id));
		   BeanUtils.copyProperties(homeSiteSearchHotKeyForm, homeSiteSearchHotKey);
		   homeSiteSearchHotKeyForm.setComAdTypeId(homeSiteSearchHotKey.getComAdType().getId());
	   }
	   
	   //加载出热键所属的板块列表
		 List li= this.getHomeSiteSearchHotKeyServ().loadComAdTypeList();
		 request.setAttribute("comAdTypeList", li);
		 
	   //获取搜索的时候的查询参数,用户修改之后返回.
	   this.loadSearchParam(request);
	   
	   request.setAttribute("homeSiteSearchHotKeyForm", homeSiteSearchHotKeyForm);
	   
	   return mapping.findForward(toPage);
	}
    
   
   /*****
    * 获取搜索参数
    * @param request
    */
     private void loadSearchParam(HttpServletRequest request) 
     {
		String bkComAdTypeId = request.getParameter("bkComAdTypeId");
		if(bkComAdTypeId!=null && !bkComAdTypeId.equals(""))
		{
			request.setAttribute("bkComAdTypeId",bkComAdTypeId );
		}
		
		String bkKeywords  =  request.getParameter("bkKeywords");
		if(bkKeywords!=null  && !bkKeywords.equals(""))
		{
			request.setAttribute("bkKeywords", bkKeywords);
		}
		
		String bkAudite   =  request.getParameter("bkAudite");
		if(bkAudite!=null  && !bkAudite.equals(""))
		{
			request.setAttribute("bkAudite",bkAudite );
		}
		
		String bkIscommend   = request.getParameter("bkIscommend");
		if(bkIscommend!=null  && !bkIscommend.equals(""))
		{
			request.setAttribute("bkIscommend", bkIscommend);
		}
		
		String bkIsDel = request.getParameter("bkIsDel");
		if(bkIsDel!=null && !bkIsDel.equals(""))
		{
			request.setAttribute("bkIsDel", bkIsDel);
		}
		
		String bkPage = request.getParameter("bkPage");
		if(bkPage!=null && !bkPage.equals(""))
		{
			request.setAttribute("bkPage", bkPage);
		}
		
		String bkPageSize = request.getParameter("bkPageSize");
		if(bkPageSize!=null && !bkPageSize.equals(""))
		{
			request.setAttribute("bkPageSize", bkPageSize);
		}
		
	}

    
     /*****
      * 前台关键字搜索
      */
     public ActionForward loadHomeSiteSearchHotKey(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception 
 	{
    	 List li = this.getHomeSiteSearchHotKeyServ().loadTopHotKeyList("资讯广告", 10);
    	 if(li!=null && !li.isEmpty())
    	 {
    		 logger.info("ok");
    	 }
    	 
    	 return null;
 	}

     /*****
      * 热门关键字搜索
      */
     public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception 
 	{
    	 String toPage="homeSiteSearchHotKeyList";
    	 HomeSiteSearchHotKeyForm  homeSiteSearchHotKeyForm = (HomeSiteSearchHotKeyForm)form;
//    	加载热门关键字列表页面.
    	 Integer page=PageContent.pageValue;
    	 Integer pageSize=PageContent.pageSizeValue;
 		this.loadHomeSiteSearchHotKeyList(request,page,pageSize,homeSiteSearchHotKeyForm);
 		
 		
 		 //加载出热键所属的板块列表
		 List li= this.getHomeSiteSearchHotKeyServ().loadComAdTypeList();
		 request.setAttribute("comAdTypeList", li);
		 
		 
 		return mapping.findForward(toPage);
    	 
 	}
     
     
	/******
      * 修改热门关键字
      */
    public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception 
	{
    	String toPage="homeSiteSearchHotKeyList";
    	 //通过id加载热门搜索关键字对象
    	String id = request.getParameter("id");
    	
    	HomeSiteSearchHotKeyForm homeSiteSearchHotKeyForm = (HomeSiteSearchHotKeyForm)form;
    	
    	if(id!=null  && !id.equals(""))
    	{
    		HomeSiteSearchHotKey homeSiteSearchHotKey = this.getHomeSiteSearchHotKeyServ().loadHomeSiteSearchHotKeyById(Integer.valueOf(id));
    		//关键字
    		homeSiteSearchHotKey.setKeywords(homeSiteSearchHotKeyForm.getKeywords());
    		//是否审核
    		homeSiteSearchHotKey.setAudite(homeSiteSearchHotKeyForm.getAudite());
    		//是否推荐
    		homeSiteSearchHotKey.setIscommend(homeSiteSearchHotKeyForm.getIscommend());
    		//是否删除
    		homeSiteSearchHotKey.setIsDel(homeSiteSearchHotKeyForm.getIsDel());
    		//链接
    		homeSiteSearchHotKey.setLink(homeSiteSearchHotKeyForm.getLink());
    		
    		//修改时间
    		homeSiteSearchHotKey.setCreateTime(new Date());
    		
    		boolean isok=this.getHomeSiteSearchHotKeyServ().updateHomeSiteSearchHotKey(homeSiteSearchHotKey);
    		
    		if(isok)
    		{
    			this.setPopMsg(request, "msg", "更新热门关键字成功!");
    		}
    		else
    		{
    			this.setPopMsg(request, "msg", "更新热门关键字失败!");
    		}
    		
    	}
    	
    	//加载关键字列表
    	//分类id
    	HomeSiteSearchHotKeyForm  homeSiteSearchHotKeyFormSearch = new HomeSiteSearchHotKeyForm(); 
    	String bkComAdTypeId = request.getParameter("bkComAdTypeId");
		if(bkComAdTypeId!=null && !bkComAdTypeId.equals(""))
		{
			homeSiteSearchHotKeyFormSearch.setComAdTypeId(Integer.valueOf(bkComAdTypeId));
		}
		
		//关键字
		String bkKeywords  =  request.getParameter("bkKeywords");
		if(bkKeywords!=null  && !bkKeywords.equals(""))
		{
			homeSiteSearchHotKeyFormSearch.setKeywords(bkKeywords);
		}
		
		//审核状态
		String bkAudite   =  request.getParameter("bkAudite");
		if(bkAudite!=null  && !bkAudite.equals(""))
		{
			homeSiteSearchHotKeyFormSearch.setAudite(Short.valueOf(bkAudite));
		}
		
		//是否推荐
		String bkIscommend   = request.getParameter("bkIscommend");
		if(bkIscommend!=null  && !bkIscommend.equals(""))
		{
			homeSiteSearchHotKeyFormSearch.setIscommend(Short.valueOf(bkIscommend));
		}
		
		//是否删除
		String bkIsDel = request.getParameter("bkIsDel");
		if(bkIsDel!=null && !bkIsDel.equals(""))
		{
			homeSiteSearchHotKeyFormSearch.setIsDel(Short.valueOf(bkIsDel));
		}
		
//		是否页码
		String bkPage = request.getParameter("bkPage");
		Integer page=PageContent.pageValue;
		if(bkPage!=null && !bkPage.equals(""))
		{
			page = Integer.valueOf(bkPage);
		}
		
//		是否页码
		String bkPageSize = request.getParameter("bkPageSize");
		Integer pageSize=PageContent.pageSizeValue;
		if(bkPageSize!=null && !bkPageSize.equals(""))
		{
		  pageSize=Integer.valueOf(bkPageSize);	
		}
    	
		
		 //加载出热键所属的板块列表
		 List li= this.getHomeSiteSearchHotKeyServ().loadComAdTypeList();
		 request.setAttribute("comAdTypeList", li);
		 
		//加载热门关键字列表页面.
		this.loadHomeSiteSearchHotKeyList(request,page,pageSize,homeSiteSearchHotKeyFormSearch);
		
		return mapping.findForward(toPage);
		
    	
	}
    
    
    

	/****
     * 名称不能为空,分类必须选择,名称不能重复.
     * @param request
     * @param homeSiteSearchHotKeyForm
     * @param string
     * @return
     */
	private boolean checkHomeSiteSearchHotKeyForm(HttpServletRequest request, HomeSiteSearchHotKeyForm homeSiteSearchHotKeyForm, String addOrUpdate) 
	{
		boolean isok=true;
		if(homeSiteSearchHotKeyForm.getKeywords()!=null && !homeSiteSearchHotKeyForm.getKeywords().equals(""))
		{
			  if(homeSiteSearchHotKeyForm.getComAdTypeId()!=null  && homeSiteSearchHotKeyForm.getComAdTypeId().intValue()!=-1)
			  {
				 
			  }
			  else
			  {
				  isok = false;
			  }
		}
		else
		{
			isok=false;
		}
		
		return isok;
	}


	public IHomeSiteSearchHotKeyServ getHomeSiteSearchHotKeyServ() {
		return homeSiteSearchHotKeyServ;
	}


	public void setHomeSiteSearchHotKeyServ(
			IHomeSiteSearchHotKeyServ homeSiteSearchHotKeyServ) {
		this.homeSiteSearchHotKeyServ = homeSiteSearchHotKeyServ;
	}


	
	
	
	
	
	  
}
